package com.gantang.gax.service.impl;

import com.gantang.gax.dto.bool.BoolFeatureTable;
import com.gantang.gax.engine.eo.Engine;
import com.gantang.gax.engine.model.EngineCreator;
import com.gantang.gax.engine.model.VariableGroup;
import com.gantang.gax.engine.service.EngineCreatorService;
import com.gantang.gax.service.BoolFeatureTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("cxdBoolFeatureTableService")
public class FeatureTableServiceImpl implements BoolFeatureTableService {
    @Autowired
    private EngineCreatorService cxdEngineCreatorService;

    @Override
    public Engine doUpsertFeatureTable(BoolFeatureTable featureTable) {
        EngineCreator engineCreator = featureTable2EngineCreator(featureTable);
        Engine engine = cxdEngineCreatorService.create(engineCreator);
        //todo 创建引擎,并存储于缓存
        return engine;
    }

    @Override
    public Object doValidateFeatureTable(BoolFeatureTable featureTable) {
        return null;
    }

    /**
     * 此处将featureTable业务转为TaskObject
     *
     * @param featureTable
     * @return
     */
    private EngineCreator featureTable2EngineCreator(BoolFeatureTable featureTable) {
        // 创建 EngineCreator 对象
        EngineCreator engineCreator = new EngineCreator();

        // 使用 Map 作为临时存储，key 为 familyCode，value 为对应的 VariableGroup
        Map<String, VariableGroup> groupMap = new HashMap<>();

        // 遍历所有 features，将同一个 familyCode 的特征归为一组
        for (BoolFeatureTable.LogicFeature feature : featureTable.getFeatures()) {
            // 获取特征的 familyCode 作为分组 key
            String familyCode = feature.getFamilyCode();

            // 获取或新建对应的 VariableGroup
            VariableGroup variableGroup = groupMap.get(familyCode);
            if (variableGroup == null) {
                // 使用 engineCreator 实例来创建内部类对象
                variableGroup = new VariableGroup();
                variableGroup.setCode(familyCode);
                // 根据实际业务，group 的 name 可以设置成 familyCode 或其他描述
                variableGroup.setName(familyCode);
                // 初始化分组内的变量列表
                variableGroup.setVariables(new ArrayList<>());
                groupMap.put(familyCode, variableGroup);
            }

            // 创建新的 Variable 对象，并设置属性（name 根据需要也可以赋值）
            VariableGroup.Variable variable = VariableGroup.Variable.builder().code(feature.getCode()).weight(feature.getWeight()).build();

            // 将 Variable 添加到对应分组内
            variableGroup.getVariables().add(variable);
        }

        // 将所有 VariableGroup 赋值给 EngineCreator 的 variableGroups 属性
        engineCreator.setVariableGroups(new ArrayList<>(groupMap.values()));

        // 处理规则，将 featureTable 中的规则转换为字符串列表
        List<String> rules = new ArrayList<>();
        for (BoolFeatureTable.LogicRule rule : featureTable.getRules()) {
            rules.add(rule.getExpression());
        }
        engineCreator.setFormulas(rules);

        return engineCreator;
    }
}
